package cc.momas.momall.service.impl;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cc.momas.common.jedis.JedisClient;
import cc.momas.common.utils.JsonUtils;
import cc.momas.common.vo.EasyUIDataGridResult;
import cc.momas.momall.mapper.TbContentMapper;
import cc.momas.momall.po.TbContent;
import cc.momas.momall.po.TbContentExample;
import cc.momas.momall.service.IContentService;

@Service("contentService")
public class ContentServiceImpl implements IContentService {

	/**
	 * 用作redis里hash的一个键,该键下缓存着所有content
	 */
	private static final String CONTENT_KEY = "content";
	@Autowired
	private TbContentMapper contentMapper;	
	@Autowired
	private JedisClient jedisClient;
	
	@Override
	public int save(TbContent content) {
		content.setUpdated(new Date());
		content.setCreated(new Date());
		// 添加广告内容
		int count = contentMapper.insert(content);
		
		//当插入数据的时候要把缓存清除,否则会查不出新内容
		jedisClient.hdel(CONTENT_KEY, String.valueOf(content.getCategoryId()));
		
		return count;
	}
	
	@Override
	public EasyUIDataGridResult getContentList(Long categoryId, Integer page, Integer rows) {
		// 设置分页信息
		PageHelper.startPage(page, rows);

		TbContentExample example=new TbContentExample();
		if(categoryId != null && categoryId != 0) {
			example.createCriteria().andCategoryIdEqualTo(categoryId);
		}
		// 查询结果
		List<TbContent> list = contentMapper.selectByExample(example);
		
//		 取分页结果
		PageInfo<TbContent> pageInfo = new PageInfo<>(list);
		long count = pageInfo.getTotal();
		
		// 创建一个返回值对象
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		result.setRows(list);
		result.setTotal(count);
		
		return result;
	}

	@Override
	public List<TbContent> getContentListByCid(Long id) {
		//先查询缓存,如果redis里面没有再去数据库里面找
		try {
			String json = jedisClient.hget(CONTENT_KEY,String.valueOf(id));
			//判断json是否为空
			if(StringUtils.isNotBlank(json)) {
				//把json转换成List
				List<TbContent> list = JsonUtils.jsonToList(json, TbContent.class);
				return list;
			}
		}catch (Exception ignore) {
		}
		
		//在redis里没有找到数据的时候在这里
		//设置查询条件
		TbContentExample example  = new TbContentExample();
		example.createCriteria().andCategoryIdEqualTo(id);
		//执行查询
		List<TbContent> list = contentMapper.selectByExampleWithBLOBs(example);
		
		try {
			jedisClient.hset(CONTENT_KEY, String.valueOf(id), JsonUtils.objectToJson(list));
		} catch (Exception ignore) {
		}
		
		return list;
	}

	@Override
	public int updateContentByIdSelective(TbContent content) {
		content.setUpdated(new Date());
		int count = contentMapper.updateByPrimaryKeySelective(content);
		
		//当更新数据的时候要把内容也插入到redis里面,以备查询缓存
		jedisClient.hset(CONTENT_KEY, String.valueOf(content.getCategoryId()),JsonUtils.objectToJson(content));
		
		return count;
	}

	@Override
	public int deleteById(String ids) {
		int count = 0;
		String [] id = ids.split(",");
		
		try {
			//这个块用来删除redis缓存
			Set<Long> tmpSet = new HashSet<>(2);
			//批量查出id对应的数据的类目id
			for (String s : id) {
				TbContent tmp = contentMapper.selectByPrimaryKey(Long.valueOf(s));
				//放到Set里去重
				tmpSet.add(tmp.getCategoryId());
			}
			// 批量删除对应的缓存
			for (Long s : tmpSet) {
				jedisClient.hdel(CONTENT_KEY, String.valueOf(s));
			}
		} catch (NumberFormatException ignore) {
		}
		
		for (String s : id) {
			count += contentMapper.deleteByPrimaryKey(Long.valueOf(s));
		}
		return count ;
	}
}
